using System.Collections.Generic;
using System.Web.Mvc;
using TestingMvcControllers.Interfaces;
using TestingMvcControllers.Models;

namespace TestingMvcControllers.Controllers
{
    public class CustomerController : Controller
    {
        private readonly ICustomerService _customerService;

        public CustomerController() { } // Parameterless constructor just in case project is run!

        public CustomerController(ICustomerService customerService)
        {
            _customerService = customerService;
        }

        public ActionResult Index()
        {
            return View("Index");
        }

        public ViewResult List(int? pageNumber, int? pageSize)
        {
            IList<Customer> customers = _customerService.Get(pageNumber, pageSize);

            ViewData["PageNumber"] = pageNumber;
            ViewData["PageSize"] = pageSize;

            return View("List", customers);
        }

        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                _customerService.Save(customer);
                return RedirectToRoute("CustomerCreated");
            }
            return View("Create", customer);
        }
    }
}